package org.homly.admin.action.product;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.homly.admin.form.ConfigForm;
import org.homly.model.Config;
import org.homly.service.ProductService;

public class SaveConfigAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ConfigForm configForm = (ConfigForm) form;
		Config config = new Config();

		BeanUtils.copyProperties(config, configForm);

		ProductService productService = new ProductService();
		if (config.getConfigId() == null || config.getConfigId().intValue() == 0) {
			productService.saveConfig(config);
		} else {
			productService.updateConfig(config);
		}

		return new ActionForward(mapping.findForward("manager_config").getPath(), true);
	}

}
